package com.cloudlead.plugin.scheduler;

import com.cloudlead.plugin.scheduler.service.ScheduleJobService;
import com.cloudlead.plugin.scheduler.vo.ScheduleJob;
import com.cloudlead.plugin.scheduler.vo.dict.JobStatus;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.quartz.SchedulerListener;
import org.quartz.Trigger;
import org.quartz.TriggerKey;

public class ScheduleJobSchedulerListener implements SchedulerListener {
	private final ScheduleJobService scheduleJobService;

	public ScheduleJobSchedulerListener(ScheduleJobService jobRepository) {
		this.scheduleJobService = jobRepository;
	}

	@Override
	public void jobScheduled(Trigger trigger) {
		System.out.println("-----------------job scheduled ["
				+ trigger.getJobKey() + "]-----------------------");
		JobKey jobKey = trigger.getJobKey();
		if (ScheduleJobService.CLOUDLEAD_SCHEDULE_JOB_GROUP.equals(jobKey
				.getGroup())) {
			ScheduleJob example = new ScheduleJob();
			example.setName(jobKey.getName());
			ScheduleJob scheduleJob = scheduleJobService.findOne(example);
			if (null != scheduleJob) {
				scheduleJob.setStatus(JobStatus.enabled);
				scheduleJob.setStartTime(System.currentTimeMillis());
				scheduleJobService.update(scheduleJob);
			}
		}
	}

	@Override
	public void jobUnscheduled(TriggerKey triggerKey) {
		System.out.println("-----------------job unscheduled [" + triggerKey
				+ "]-----------------------");

		if (ScheduleJobService.CLOUDLEAD_SCHEDULE_JOB_GROUP.equals(triggerKey
				.getGroup())) {
			ScheduleJob example = new ScheduleJob();
			example.setName(triggerKey.getName());
			ScheduleJob scheduleJob = scheduleJobService.findOne(example);
			if (null != scheduleJob) {
				scheduleJob.setStatus(JobStatus.disabled);
				scheduleJobService.update(scheduleJob);
			}
		}
	}

	@Override
	public void triggerFinalized(Trigger trigger) {
		System.out.println("-----------------trigger finalized ["
				+ trigger.getJobKey() + "]-----------------------");
		if (ScheduleJobService.CLOUDLEAD_SCHEDULE_JOB_GROUP.equals(trigger
				.getJobKey().getGroup())) {

			ScheduleJob example = new ScheduleJob();
			example.setName(trigger.getJobKey().getName());
			ScheduleJob scheduleJob = scheduleJobService.findOne(example);
			if (null != scheduleJob) {
				scheduleJob.setStatus(JobStatus.completed);
				scheduleJob.setEndTime(System.currentTimeMillis());
				scheduleJobService.update(scheduleJob);
			}
		}
	}

	@Override
	public void triggerPaused(TriggerKey triggerKey) {
		System.out.println("-----------------trigger paused ["
				+ triggerKey.getName() + "]-----------------------");
		if (ScheduleJobService.CLOUDLEAD_SCHEDULE_JOB_GROUP.equals(triggerKey
				.getGroup())) {
			ScheduleJob example = new ScheduleJob();
			example.setName(triggerKey.getName());
			ScheduleJob scheduleJob = scheduleJobService.findOne(example);
			if (null != scheduleJob) {
				scheduleJob.setStatus(JobStatus.paused);
				scheduleJobService.update(scheduleJob);
			}
		}

	}

	@Override
	public void triggersPaused(String triggerGroup) {

	}

	@Override
	public void triggerResumed(TriggerKey triggerKey) {
		System.out.println("-----------------trigger resumed ["
				+ triggerKey.getName() + "]-----------------------");
		if (ScheduleJobService.CLOUDLEAD_SCHEDULE_JOB_GROUP.equals(triggerKey
				.getGroup())) {
			ScheduleJob example = new ScheduleJob();
			example.setName(triggerKey.getName());
			ScheduleJob scheduleJob = scheduleJobService.findOne(example);
			if (null != scheduleJob) {
				scheduleJob.setStatus(JobStatus.enabled);
				scheduleJobService.update(scheduleJob);
			}
		}
	}

	@Override
	public void triggersResumed(String triggerGroup) {

	}

	@Override
	public void jobAdded(JobDetail jobDetail) {
		System.out.println("-----------------job add [" + jobDetail.getKey()
				+ "]-----------------------");

	}

	@Override
	public void jobDeleted(JobKey jobKey) {
		System.out.println("-----------------job deleted [" + jobKey
				+ "]-----------------------");
	}

	@Override
	public void jobPaused(JobKey jobKey) {
		System.out.println("-----------------job paused [" + jobKey
				+ "]-----------------------");
	}

	@Override
	public void jobsPaused(String jobGroup) {

	}

	@Override
	public void jobResumed(JobKey jobKey) {
		System.out.println("-----------------job resumed [" + jobKey
				+ "]-----------------------");
	}

	@Override
	public void jobsResumed(String jobGroup) {

	}

	@Override
	public void schedulerError(String msg, SchedulerException cause) {

	}

	@Override
	public void schedulerInStandbyMode() {

	}

	@Override
	public void schedulerStarted() {

	}

	@Override
	public void schedulerStarting() {

	}

	@Override
	public void schedulerShutdown() {

	}

	@Override
	public void schedulerShuttingdown() {

	}

	@Override
	public void schedulingDataCleared() {

	}

}
